package com.yingxin.yxerp.module.attachment.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yxerp.module.attachment.entity.AttachmentRelation;
import com.yingxin.yxerp.module.attachment.mapper.AttachmentRelationMapper;
import com.yingxin.yxerp.module.attachment.service.IAttachmentRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 附件关系表 服务实现类
 * @author jiangxudong
 * @since 2022-03-28
 */

@Service
public class AttachmentRelationServiceImpl
        extends ServiceImpl<AttachmentRelationMapper, AttachmentRelation> implements
        IAttachmentRelationService {


    @Override
    public void create(String attachmentId, String bizId, String bizType, String bizAttr) {
        save(new AttachmentRelation(attachmentId, bizId, bizType, bizAttr));
    }

    @Override
    public void delete(String bizId, BusinessType bizType, String bizAttr) {
        UpdateWrapper<AttachmentRelation> uw = new UpdateWrapper<>();
        uw.lambda().eq(AttachmentRelation::getBusinessId, bizId);
        if (StringUtils.isNotBlank(bizType.getType())) {
            uw.lambda().eq(AttachmentRelation::getBusinessType, bizType.getType());
        }
        if (StringUtils.isNotBlank(bizAttr)) {
            uw.lambda().eq(AttachmentRelation::getBusinessAttr, bizAttr);
        }
        remove(uw);
    }
}